/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.shiep.aurora.calendar;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.text.DateFormatter;
import work02.w5.control.Schedule;
import work02.w5.model.ScheduleFrequence;
import work02.w5.model.ScheduleModel;

/**
 *
 * @author Eric
 */
public class SchedulePanel extends javax.swing.JPanel {

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    DateFormatter df = new DateFormatter(format);
    DateFormat format2 = new SimpleDateFormat("HH:mm");
    DateFormatter df2 = new DateFormatter(format2);
    String[] advancedNoticeMilisecondModel = new String[]{"10分钟", "15分钟", "30分钟", "45分钟", "1小时", "2小时", "3小时", "5小时", "6小时", "12小时", "1天", "2天", "一周"};
    String[] frequenceJComboBox2Model = new String[]{"一次性活动", "每天", "每周", "每月", "每年"};
    BaseFrame frame;
    private State stating = State.New;
    /**
     * 上下文，用于在修改界面时得知修改前的样式
     */
    private ScheduleModel contextScheduleModel;

    public enum State {

        Change, Read, New
    }

    /**
     * Creates new form SchedulePanel
     */
    public SchedulePanel() {
        initComponents();

    }
    private CalendarFrameAdapter calFrameAdapter;

    public void IOC(SchedulePanelAdapter adapter, Schedule scheduleControl, CalendarFrameAdapter cal) {
        this.adapter = adapter;
        this.scheduleControl = scheduleControl;
        frame = new BaseFrame(this);
        frame.setFocusable(true);
        frame.setVisible(true);
        frame.setSize(470, 520);
        frame.setVisible(true);
        this.setVisible(false);
        this.calFrameAdapter = cal;

    }

    /**
     * 刷新界面状态，一共有3个状态，只读，修改，新文件
     */
    public void setState(SchedulePanel.State state) {
        stating = state;
        switch (state) {
            case Change:
                scheduleNameJTextField1.setEditable(true);
                startDateJFormattedTextField.setEditable(true);
                startTimeJFormattedTextField.setEditable(true);
                endDateJFormattedTextField.setEditable(true);
                endTimeJFormattedTextField.setEditable(true);
                remarkJTextArea1.setEditable(true);
                advancedNoticeMilisecondJComboBox1.setEditable(true);
                frequenceJComboBox2.setEditable(true);
                cancelButtonJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/rubbishAndcancelButton.png"))); // NOI18N
                break;
            case New:
                scheduleNameJTextField1.setEditable(true);
                startDateJFormattedTextField.setEditable(true);
                startTimeJFormattedTextField.setEditable(true);
                endDateJFormattedTextField.setEditable(true);
                endTimeJFormattedTextField.setEditable(true);
                remarkJTextArea1.setEditable(true);
                advancedNoticeMilisecondJComboBox1.setEditable(true);
                frequenceJComboBox2.setEditable(true);
                cancelButtonJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/cancel.png"))); // NOI18N
                break;
            case Read:
                scheduleNameJTextField1.setEditable(false);
                startDateJFormattedTextField.setEditable(false);
                startTimeJFormattedTextField.setEditable(false);
                endDateJFormattedTextField.setEditable(false);
                endTimeJFormattedTextField.setEditable(false);
                remarkJTextArea1.setEditable(false);
                advancedNoticeMilisecondJComboBox1.setEditable(false);
                frequenceJComboBox2.setEditable(false);
                cancelButtonJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/rubbishAndcancelButton.png"))); // NOI18N
                break;
        }

    }

    public void setContent(ScheduleModel m,State state) {
        contextScheduleModel = m;
        scheduleNameJTextField1.setText(m.getName());
        startDateJFormattedTextField.setText(format.format(m.getStartTime()));
        startTimeJFormattedTextField.setText(format2.format(m.getStartTime()));
        endDateJFormattedTextField.setText(format.format(m.getEndTime()));
        endTimeJFormattedTextField.setText(format2.format(m.getEndTime()));
        remarkJTextArea1.setText(m.getRemark());
        advancedNoticeMilisecondJComboBox1.setSelectedIndex(adapter.conventAdvancedNoticeMilisecondToModelIndex(m.getAdvancedNoticeMilisecond(), advancedNoticeMilisecondModel));
        frequenceJComboBox2.setSelectedIndex(adapter.conventScheduleFrequenceToModelIndex(m.getFrequence(), advancedNoticeMilisecondModel));
        setState(state);
    }

    public ScheduleModel getContent() {
        String name = scheduleNameJTextField1.getText();
        Date startDate, endDate;
        try {
            startDate = dateFormat.parse(startDateJFormattedTextField.getText() + " " + startTimeJFormattedTextField.getText());
        } catch (ParseException ex) {
            startDate = new Date();
        }
        try {
            endDate = dateFormat.parse(endDateJFormattedTextField.getText() + " " + endTimeJFormattedTextField.getText());
        } catch (ParseException ex) {
            endDate = new Date();
        }

        String remark = remarkJTextArea1.getText();
        long advance = adapter.conventModelIndexToAdvancedNoticeMilisecond(advancedNoticeMilisecondJComboBox1.getSelectedIndex(), advancedNoticeMilisecondModel);
        ScheduleFrequence fre = adapter.conventModelIndexToScheduleFrequence(frequenceJComboBox2.getSelectedIndex(), frequenceJComboBox2Model);
        ScheduleModel m = new ScheduleModel(startDate, endDate, name, remark, advance, fre);
        return m;
    }
    private SchedulePanelAdapter adapter;
    private Schedule scheduleControl;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        scheduleNameJTextField1 = new javax.swing.JTextField();
        startDateJFormattedTextField = new javax.swing.JFormattedTextField(df);
        startTimeJFormattedTextField = new javax.swing.JFormattedTextField(df2);
        startDateJLabel2 = new javax.swing.JLabel();
        endDateJFormattedTextField = new javax.swing.JFormattedTextField(df);
        endTimeJFormattedTextField = new javax.swing.JFormattedTextField(df2);
        endDateJLabel = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        remarkJTextArea1 = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        advancedNoticeMilisecondJComboBox1 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        frequenceJComboBox2 = new javax.swing.JComboBox();
        confirmButtonjLabel = new javax.swing.JLabel();
        cancelButtonJLabel = new javax.swing.JLabel();
        backGroundJLabel = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(300, 480));
        setMinimumSize(new java.awt.Dimension(300, 480));
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(300, 480));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel4.setFont(new java.awt.Font("微软雅黑", 0, 15)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(247, 247, 247));
        jLabel4.setText("活动");
        add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 30, -1, -1));
        add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 200, 260, 10));

        scheduleNameJTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scheduleNameJTextField1ActionPerformed(evt);
            }
        });
        add(scheduleNameJTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, 240, 20));

        startDateJFormattedTextField.setText("2012-10-1");
        add(startDateJFormattedTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 110, 150, -1));

        startTimeJFormattedTextField.setText("00:00");
        add(startTimeJFormattedTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 110, 80, -1));

        startDateJLabel2.setFont(new java.awt.Font("微软雅黑", 0, 15)); // NOI18N
        startDateJLabel2.setForeground(new java.awt.Color(247, 247, 247));
        startDateJLabel2.setText("开始日期");
        add(startDateJLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 90, -1, -1));

        endDateJFormattedTextField.setText("2012-10-1");
        add(endDateJFormattedTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 160, 150, -1));

        endTimeJFormattedTextField.setText("00:00");
        add(endTimeJFormattedTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 160, 80, -1));

        endDateJLabel.setFont(new java.awt.Font("微软雅黑", 0, 15)); // NOI18N
        endDateJLabel.setForeground(new java.awt.Color(247, 247, 247));
        endDateJLabel.setText("结束日期");
        add(endDateJLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 140, -1, -1));
        add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 330, 260, 10));

        jScrollPane1.setFocusable(false);
        jScrollPane1.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N

        remarkJTextArea1.setLineWrap(true);
        remarkJTextArea1.setColumns(20);
        remarkJTextArea1.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
        remarkJTextArea1.setRows(5);
        jScrollPane1.setViewportView(remarkJTextArea1);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 230, 240, 90));

        jLabel5.setFont(new java.awt.Font("微软雅黑", 0, 15)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(247, 247, 247));
        jLabel5.setText("备注");
        add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 210, -1, -1));
        add(jSeparator3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 80, 260, 10));

        jLabel3.setFont(new java.awt.Font("微软雅黑", 0, 15)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(247, 247, 247));
        jLabel3.setText("提醒");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 340, -1, -1));

        advancedNoticeMilisecondJComboBox1.setFont(new java.awt.Font("微软雅黑", 0, 15)); // NOI18N
        advancedNoticeMilisecondJComboBox1.setModel(new javax.swing.DefaultComboBoxModel(advancedNoticeMilisecondModel));
        add(advancedNoticeMilisecondJComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 370, 90, -1));

        jLabel6.setFont(new java.awt.Font("微软雅黑", 0, 15)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(247, 247, 247));
        jLabel6.setText("活动频率");
        add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 340, 60, -1));

        frequenceJComboBox2.setFont(new java.awt.Font("微软雅黑", 0, 15)); // NOI18N
        frequenceJComboBox2.setModel(new javax.swing.DefaultComboBoxModel(frequenceJComboBox2Model));
        add(frequenceJComboBox2, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 370, 120, -1));

        confirmButtonjLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/confirm.png"))); // NOI18N
        confirmButtonjLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                confirmButtonjLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                confirmButtonjLabelMouseEntered(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                confirmButtonjLabelMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                confirmButtonjLabelMouseReleased(evt);
            }
        });
        add(confirmButtonjLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 410, -1, -1));

        cancelButtonJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/cancel.png"))); // NOI18N
        cancelButtonJLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonJLabelMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                cancelButtonJLabelMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                cancelButtonJLabelMouseReleased(evt);
            }
        });
        add(cancelButtonJLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 410, -1, -1));

        backGroundJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/scheduleBackGround.png"))); // NOI18N
        add(backGroundJLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void scheduleNameJTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scheduleNameJTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_scheduleNameJTextField1ActionPerformed

    private void cancelButtonJLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonJLabelMouseClicked
        switch (stating) {
            case Read:
                try {
                    scheduleControl.removeShcedule(contextScheduleModel);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "写入错误！" + "/n ErrorCode /n " + e.getMessage());
                }
                break;
            case New:
                break;
            case Change:
                try {
                    scheduleControl.removeShcedule(contextScheduleModel);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "写入错误！" + "/n ErrorCode /n " + e.getMessage());
                }
                break;
        }
        this.setVisible(false);
        calFrameAdapter.refreshCalendar();
   }//GEN-LAST:event_cancelButtonJLabelMouseClicked

    private void confirmButtonjLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_confirmButtonjLabelMouseClicked

        switch (stating) {
            case Change:
                try {
                    scheduleControl.changeSchedule(getContent(), contextScheduleModel);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "写入错误！" + "/n ErrorCode /n " + e.getMessage());
                }
                break;
            case New:
                try {
                    scheduleControl.addSchedule(getContent());
                } catch (Exception e) {
                    JOptionPane.showConfirmDialog(this, "写入错误！" + "/n ErrorCode /n " + e.getMessage());
                }
                break;
            case Read:
                break;

        }
        this.setVisible(false);
        calFrameAdapter.refreshCalendar();
   }//GEN-LAST:event_confirmButtonjLabelMouseClicked

    private void confirmButtonjLabelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_confirmButtonjLabelMousePressed
        confirmButtonjLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/confirmPressed.png")));
    }//GEN-LAST:event_confirmButtonjLabelMousePressed

    private void confirmButtonjLabelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_confirmButtonjLabelMouseReleased
        confirmButtonjLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/confirm.png")));
    }//GEN-LAST:event_confirmButtonjLabelMouseReleased

    private void cancelButtonJLabelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonJLabelMousePressed
        switch (stating) {
            case Read:
            case Change:
                cancelButtonJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/rubbishPressedAndcancelButton.png"))); // NOI18N
                break;
            case New:
                cancelButtonJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/cancelPressed.png")));
                break;
        }
  
    }//GEN-LAST:event_cancelButtonJLabelMousePressed
    private void cancelButtonJLabelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonJLabelMouseReleased
            switch (stating) {
            case Read:
            case Change:
                cancelButtonJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/rubbishAndcancelButton.png"))); // NOI18N
                break;
            case New:
                cancelButtonJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/cancel.png")));
                break;
        }
    }//GEN-LAST:event_cancelButtonJLabelMouseReleased

    private void confirmButtonjLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_confirmButtonjLabelMouseEntered
        // TODO add your handling code here:
    }//GEN-LAST:event_confirmButtonjLabelMouseEntered
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox advancedNoticeMilisecondJComboBox1;
    private javax.swing.JLabel backGroundJLabel;
    private javax.swing.JLabel cancelButtonJLabel;
    private javax.swing.JLabel confirmButtonjLabel;
    private javax.swing.JFormattedTextField endDateJFormattedTextField;
    private javax.swing.JLabel endDateJLabel;
    private javax.swing.JFormattedTextField endTimeJFormattedTextField;
    private javax.swing.JComboBox frequenceJComboBox2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTextArea remarkJTextArea1;
    private javax.swing.JTextField scheduleNameJTextField1;
    private javax.swing.JFormattedTextField startDateJFormattedTextField;
    private javax.swing.JLabel startDateJLabel2;
    private javax.swing.JFormattedTextField startTimeJFormattedTextField;
    // End of variables declaration//GEN-END:variables
}
